package com.earnings.okhttputils.utils.god;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.AbsListView;
import android.widget.CheckedTextView;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.earnings.R;
import com.earnings.okhttputils.utils.god.Listener.GodAnimationListener;
import com.earnings.okhttputils.utils.god.Listener.GodOnClickListener;
import com.earnings.okhttputils.utils.god.Listener.GodOnLongClickListener;
import com.earnings.okhttputils.utils.god.Listener.GodOnTouchListener;
import com.earnings.okhttputils.utils.ui.activity.DetailActivity;
import com.earnings.okhttputils.utils.ui.dialog.DetailDialog;

/**
 * Created by Administrator on 2017/5/16 0016.
 */

public abstract class GodLeftHandBaseActivity extends GodBaseActivity implements View.OnLongClickListener, View.OnTouchListener, View.OnClickListener {
    private GodOnClickListener mGodOnClickListener;
    private GodOnLongClickListener mGodOnLongClickListener;
    private GodOnTouchListener mGodOnTouchListener;
    private GodAnimationListener mGodAnimationListener;
    private SparseArray<View> views = new SparseArray<>();

    protected TextView title_tv, top_right_tv;
    protected ViewGroup root_view;
    protected View top_view, top_xian_view;
    private DetailDialog detailDialog;
    protected ImageView top_img, back_view;
    protected View back_views;

    @Override
    protected void onGodCreate(Bundle savedInstanceState) {
        setContentView(R.layout.base_layout);
        baseInitView();

    }

    private void baseInitView() {
        root_view = (ViewGroup) findViewById(R.id.root_view);
        title_tv = (TextView) findViewById(R.id.title_tv);
        top_right_tv = (TextView) findViewById(R.id.top_right_tv);
        back_view = (ImageView) findViewById(R.id.back_view);
        top_img = (ImageView) findViewById(R.id.top_img);
        top_xian_view = findViewById(R.id.top_xian_view);
        top_view = findViewById(R.id.top_view);

        View view = getLayoutInflater().inflate(onCreateLayoutId(), null, false);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(layoutParams);
        root_view.addView(view);
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        // if (this instanceof DetailActivity) top_img.setVisibility(View.VISIBLE);
        if (this instanceof DetailActivity) top_right_tv.setVisibility(View.VISIBLE);

        top_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (detailDialog == null) {
                    detailDialog = new DetailDialog(GodLeftHandBaseActivity.this);
                }
                detailDialog.show();
            }
        });

    }

    protected void changeTop() {
        top_view.setBackgroundColor(getResources().getColor(R.color.style));
        back_view.setImageResource(R.mipmap.nav_back02);
        title_tv.setTextColor(getResources().getColor(R.color.white));
        top_right_tv.setTextColor(getResources().getColor(R.color.white));

    }

    protected void setTitle(String title) {
        title_tv.setText(title);
    }

    protected void setTopTextColor(View view, int resources) {
        if (view instanceof TextView) {
            ((TextView) view).setTextColor(resources);
        }
    }

    protected void setTopVisibility(View view, int visibility) {
        switch (visibility) {
            case View.INVISIBLE:
                view.setVisibility(View.INVISIBLE);
                break;
            case View.GONE:
                view.setVisibility(View.GONE);
                break;
            case View.VISIBLE:
                view.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }

    }

    protected void setTopVisibility(int id, int visibility) {
        View view = getView(id);
        switch (visibility) {
            case View.INVISIBLE:
                view.setVisibility(View.INVISIBLE);
                break;
            case View.GONE:
                view.setVisibility(View.GONE);
                break;
            case View.VISIBLE:
                view.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }

    }

    protected void setTopImageResource(ImageView back_view, int ImageResource) {
        back_view.setImageResource(ImageResource);
    }

    protected void setTopStyleView() {
        back_view.setImageResource(R.mipmap.back_white);
        //setImageDrawable( R.id.upback_view,R.mipmap.back_white );
        top_view.setBackgroundColor(getResources().getColor(R.color.style));
        title_tv.setTextColor(getResources().getColor(R.color.white));
        top_right_tv.setTextColor(getResources().getColor(R.color.white));
        top_xian_view.setVisibility(View.GONE);
        //  back_view.setImageResource(R.);
    }

    public Context getContext() {
        return this;
    }

    protected void setTopViewBackgroundResource(int color) {
        top_view.setBackgroundColor(color);
    }

    public int getIdFromXML(String name) {
        return getResources().getIdentifier(name, "id", getPackageName());
    }

    public int getIdFromXML(View view, String name) {
        return view.getResources().getIdentifier(name, "id", getPackageName());
    }


    public ImageView setGlideImagView(int id, String url) {
        ImageView imageView = getView(id);
        Glide.with(this).load(url).into(imageView);
        return imageView;
    }

    public ImageView setGlideImagView(int id, String url, int errorid) {
        ImageView imageView = getView(id);
        Glide.with(this).load(url).error(errorid).into(imageView);
        return imageView;
    }

    public int getDrawFromXML(String name) {
        return getResources().getIdentifier(name, "drawable", getPackageName());
    }

    public int getStringFromXML(String name) {
        return getResources().getIdentifier(name, "string", getPackageName());
    }

    protected void showRightTextView() {
        top_right_tv.setVisibility(View.VISIBLE);
    }

    protected void showRightTextView(String text) {
        top_right_tv.setText(text);
        top_right_tv.setVisibility(View.VISIBLE);
    }

    protected void showRightImage(int imgid) {
        top_img.setImageResource(imgid);
        top_img.setVisibility(View.VISIBLE);
    }

    protected void setClickable(int id, boolean clickable) {
        View view = getView(id);
        view.setClickable(clickable);
    }

    protected void HideRightTextView() {
        top_right_tv.setVisibility(View.GONE);
    }

    protected void setHideTopView() {
        top_view.setVisibility(View.GONE);
        top_xian_view.setVisibility(View.GONE);
    }

    protected void setShowTopView() {
        top_view.setVisibility(View.VISIBLE);
    }

    protected abstract int onCreateLayoutId();

    protected TextView setText(int viewId, String text) {
        TextView textView = getView(viewId);
        textView.setText(text);
        return textView;
    }

    protected TextView setTextColor(int viewId, int colorId) {
        TextView textView = getView(viewId);
        textView.setTextColor(colorId);
        return textView;
    }

    protected RatingBar setRating(int viewId, float rating, int max) {
        RatingBar view = getView(viewId);
        view.setMax(max);
        view.setRating(rating);
        return view;
    }

    protected RatingBar setRating(int viewId, float rating) {
        RatingBar view = getView(viewId);
        view.setRating(rating);
        return view;
    }

    protected View setBackgroundColor(int viewId, int colorId) {
        View view = getView(viewId);
        view.setBackgroundColor(colorId);
        return view;
    }

    protected ImageView setImageDrawable(int viewId, Drawable drawable) {
        ImageView view = getView(viewId);
        view.setImageDrawable(drawable);
        return view;
    }

    protected ImageView setImageDrawable(int viewId, int imgid) {
        ImageView view = getView(viewId);
        view.setImageResource(imgid);
        return view;
    }

    protected ImageView setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bitmap);
        return view;
    }


    protected View setBackgroundResource(int viewId, int backgroundResource) {
        View view = getView(viewId);
        view.setBackgroundResource(backgroundResource);
        return view;
    }

    protected View setAlphaAnimation(int viewId, float fromAlpha, float toAlpha, long duration) {
        View view = getView(viewId);
        Animation animation = new AlphaAnimation(fromAlpha, toAlpha);
        animation.setDuration(duration);
        view.startAnimation(animation);
        return view;
    }

    protected View setAlphaAnimation(int viewId, float fromAlpha, float toAlpha, long duration, GodAnimationListener animationListener) {
        this.mGodAnimationListener = animationListener;
        View view = getView(viewId);
        Animation animation = new AlphaAnimation(fromAlpha, toAlpha);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                mGodAnimationListener.onAnimationStart(animation);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mGodAnimationListener.onAnimationEnd(animation);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                mGodAnimationListener.onAnimationRepeat(animation);
            }
        });
        animation.setDuration(duration);
        view.startAnimation(animation);
        return view;
    }

    protected View setVisible(int viewId, boolean visible) {
        View view = getView(viewId);
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
        return view;
    }

    protected View setVisibility(int viewId, int visible) {
        View view = getView(viewId);
        switch (visible) {
            case View.VISIBLE:
                view.setVisibility(View.VISIBLE);
                break;
            case View.INVISIBLE:
                view.setVisibility(View.INVISIBLE);
                break;
            case View.GONE:
                view.setVisibility(View.GONE);
                break;
            default:
                break;
        }
        return view;
    }

    protected ImageView setImageResource(int viewId, int imageResId) {
        ImageView view = getView(viewId);
        view.setImageResource(imageResId);
        return view;
    }

    protected ProgressBar setProgress(int viewId, int progress) {
        ProgressBar view = getView(viewId);
        view.setProgress(progress);
        return view;
    }

    protected View setTag(int viewId, Object tag) {
        View view = getView(viewId);
        view.setTag(tag);
        return view;
    }

    protected View setTag(int viewId, int key, Object tag) {
        View view = getView(viewId);
        view.setTag(key, tag);
        return view;
    }

    protected View setChecked(int viewId, boolean checked) {
        View view = getView(viewId);
        if (view instanceof CompoundButton) {
            ((CompoundButton) view).setChecked(checked);
        } else if (view instanceof CheckedTextView) {
            ((CheckedTextView) view).setChecked(checked);
        }
        return view;
    }

    protected View setListViewAdapter(int viewId, ListAdapter adapter) {
        View view = getView(viewId);
        if (view instanceof AbsListView) {
            ((ListView) view).setAdapter(adapter);
        }
        return (ListView) view;
    }

    protected View setRecyclerViewAdapter(int viewId, RecyclerView.Adapter adapter) {
        View view = getView(viewId);
        if (view instanceof RecyclerView) {
            ((RecyclerView) view).setAdapter(adapter);
        }
        return (RecyclerView) view;
    }

    protected RecyclerView setRecyclerViewLayoutManager(int viewId, RecyclerView.LayoutManager layoutManager) {
        View view = getView(viewId);
        if (view instanceof RecyclerView) {
            ((RecyclerView) view).setLayoutManager(layoutManager);
        }
        return (RecyclerView) view;
    }

    protected View scrollTo(int viewId, int x, int y) {
        View view = getView(viewId);
        view.scrollTo(x, y);
        return view;
    }

    protected void setOnClickListener(GodOnClickListener godOnClickListener, int... viewIds) {
        this.mGodOnClickListener = godOnClickListener;
        for (int viewId : viewIds) {
            View view = getView(viewId);
            view.setOnClickListener(this);

        }
    }


    protected void setOnTouchListener(GodOnTouchListener mGodOnTouchListener, int... viewIds) {
        this.mGodOnTouchListener = mGodOnTouchListener;
        for (int viewId : viewIds) {
            View view = getView(viewId);
            view.setOnTouchListener(this);
        }
    }

    protected void setOnLongClickListener(final GodOnLongClickListener mGodOnLongClickListener, int... viewIds) {
        this.mGodOnLongClickListener = mGodOnLongClickListener;
        for (int viewId : viewIds) {
            View view = getView(viewId);
            view.setOnLongClickListener(this);

        }
    }

    public <T extends View> T getView(int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            view = findViewById(viewId);
            views.put(viewId, view);
        }
        return (T) view;
    }

    public TextView getTextView(int viewId) {
        TextView view = (TextView) views.get(viewId);
        if (view == null) {
            view = (TextView) findViewById(viewId);
            views.put(viewId, view);
        }
        return view;
    }

    public EditText getEditText(int viewId) {
        EditText view = (EditText) views.get(viewId);
        if (view == null) {
            view = (EditText) findViewById(viewId);
            views.put(viewId, view);
        }
        return view;
    }

    @Override
    public void onClick(View v) {
        if (mGodOnClickListener != null) {
            mGodOnClickListener.OnClickListener(v);
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (mGodOnLongClickListener != null) {
            mGodOnLongClickListener.OnLongClickListener(v);
        }
        return true;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (mGodOnTouchListener != null) {
            mGodOnTouchListener.OnTouchListener(v, event);
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        views = null;
    }
}
